﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class ResultEntry : System.Web.UI.Page
    {
        StudentResultManager manager=new StudentResultManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }
            if (IsPostBack!=true)
            {
                PopulatedGradeDropDownList();
            }

        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            string regno = regNoTextBox.Text;
            Student aStudent = manager.SearchStudentByRegNo(regno);
            nameTextBox.Text = aStudent.Name;
            emailTextBox.Text = aStudent.Email;
            departmentTextBox.Text = aStudent.Department.Name;
            departmentIdLabel.Text = aStudent.Department.DeptId.ToString();
            PopulatedCourseDropDownList();
            
        }

        private void PopulatedGradeDropDownList()
        {
            GradeManager gradeManager =new GradeManager();
            DataTable gradeTable= gradeManager.GetAllGrade();
            gradeLetterDropDownList.DataTextField = "GradeLetter";
            gradeLetterDropDownList.DataValueField = "Id";
            gradeLetterDropDownList.DataSource = gradeTable;
            gradeLetterDropDownList.DataBind();

        }

        private void PopulatedCourseDropDownList()
        {
            string regNo = regNoTextBox.Text;
            DataTable coursesTable = manager.GetAllEnrollCourseForCurrentStudent(regNo);
            enrollCoursesDropDownList.DataTextField = "Code";
            enrollCoursesDropDownList.DataValueField = "Id";
            enrollCoursesDropDownList.DataSource = coursesTable;
            enrollCoursesDropDownList.DataBind();
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            Student aStudent=new Student();
            try
            {

                aStudent.RegNo = regNoTextBox.Text;
                aStudent.Name = nameTextBox.Text;
                aStudent.Email = emailTextBox.Text;
                aStudent.Course.CourseId = Convert.ToInt32(enrollCoursesDropDownList.SelectedValue);
                aStudent.Course.Code = enrollCoursesDropDownList.SelectedItem.ToString();
                aStudent.Department.DeptId = Convert.ToInt32(departmentIdLabel.Text);
                aStudent.GradeId = Convert.ToInt32(gradeLetterDropDownList.SelectedValue);
                aStudent.Grade = gradeLetterDropDownList.SelectedItem.ToString();
                string message = manager.SaveStudentResult(aStudent);
                messageLabel.ForeColor = Color.Green;
                messageLabel.Text = message;

            }
                catch(FormatException exception)
                {

                    messageLabel.ForeColor = Color.Red;
                    messageLabel.Text = exception.Message;

                }
            catch (Exception exception)
            {

                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }
            
           
        }

    }
}